Typical HAL UART use cases for migration ¶
This section provides information about typical HAL UART use cases for migration.
Note
The processes for transmitting and receiving data using polling, interrupt or DMA are the same in HAL1 and HAL2, except for the specification of buffer size: HAL1 uses
Size
for the number of data elements (u8 or u16),
while HAL2 uses
Size_byte
for the buffer size in bytes.
Use case: UART communication in polling mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND_SIZE 256
#define DATA_TO_RECEIVE_SIZE DATA_TO_SEND_SIZE
uint8_t aTxBuffer[] = " **** UART Communication **** ";
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];
UART_HandleTypeDef huart;
HAL_UART_Init(&huart)
HAL_UART_Receive(&huart, aRxBuffer,
DATA_TO_RECEIVE_SIZE, 100)
HAL_UART_Transmit(&huart, aTxBuffer,
DATA_TO_SEND_SIZE, 100)
|
#define DATA_TO_SEND_SIZE 256
#define DATA_TO_RECEIVE_SIZE DATA_TO_SEND_SIZE
uint8_t aTxBuffer[] = " **** UART Communication **** ";
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];
hal_uart_handle_t huart;
hal_uart_config_t uart_config;
HAL_UART_Init(&huart, HAL_UART4)
/* add config for uart instance */
HAL_UART_SetConfig(&huart, &uart_config)
HAL_UART_Receive(&huart, aRxBuffer,
DATA_TO_RECEIVE_SIZE, 100)
HAL_UART_Transmit(&huart,aTxBuffer,
DATA_TO_SEND_SIZE, 100)
|
Key changes ¶
In HAL2, the UART instance is provided to
HAL_UART_Init()and configuration is separated usingHAL_UART_SetConfig().
Use case: UART RX in interrupt mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_RECEIVE_SIZE 256
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];
UART_HandleTypeDef huart;
/* Ensure that all required peripherals
(USART, GPIO,NVIC etc.) are configured properly
/*
...
*/
/* UART register Receive complete user callback */
HAL_UART_RegisterCallback(&huart, HAL_UART_RX_CB_ID,
UART_RxCpltCallback);
/* Start a UART Process in interruption mode */
HAL_UART_Receive_IT(&huart, aRxBuffer,
DATA_TO_RECEIVE_SIZE)
|
#define DATA_TO_RECEIVE_SIZE 256
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];
hal_uart_handle_t huart;
/* Ensure that all required peripherals
(USART, GPIO,NVIC etc.) are configured properly
/*
...
*/
/* UART register Receive complete user callback */
HAL_UART_RegisterRxCpltCallback(&huart, UART_RxCpltCallback);
/* Start a UART Process in interruption mode */
HAL_UART_Receive_IT(&huart, aRxBuffer,
DATA_TO_RECEIVE_SIZE)
|
Key changes ¶
HAL2 provides a dedicated registration API for RX completion callbacks:
HAL_UART_RegisterRxCpltCallback().HAL2 RX completion callback includes an RX event argument (for example
HAL_UART_RXEVENT_TC).
Use case: UART TX in interrupt mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND_SIZE 256
uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** UART Communication **** ";
UART_HandleTypeDef huart;
/* Ensure that all required peripherals
(USART, GPIO,NVIC etc.) are configured properly
/*
...
*/
/* UART register Transmit complete user callback */
HAL_UART_RegisterCallback(&huart, HAL_UART_TX_CB_ID,
UART_TxCpltCallback);
/* Start a UART Process in interruption mode */
HAL_UART_Transmit_IT(&huart, aTxBuffer,
DATA_TO_SEND_SIZE)
|
#define DATA_TO_SEND_SIZE 256
uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** UART Communication **** ";
hal_uart_handle_t huart;
/* Ensure that all required peripherals
(USART, GPIO,NVIC etc.) are configured properly
/*
...
*/
/* UART register Transmit complete user callback */
HAL_UART_RegisterTxCpltCallback(&huart, UART_TxCpltCallback);
/* Start a UART Process in interruption mode */
HAL_UART_Transmit_IT(&huart, aTxBuffer,
DATA_TO_SEND_SIZE)
|
Key changes ¶
HAL2 provides a dedicated registration API for TX completion callbacks:
HAL_UART_RegisterTxCpltCallback()..
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
hal_uart_two_boards_com_it_controller |
How to handle an infinite number of transmit-receive transactions between two boards based on the UART-bus protocol with the HAL API, in interrupt mode. |
|
hal_uart_two_boards_com_it_responder |
How to respond in an interrupt mode UART communication, driven by the controller, using the HAL API. |
Use case: UART RX in DMA mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_RECEIVE_SIZE 255
UART_HandleTypeDef huart;
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];
/* Ensure that all required peripherals
(USART, GPIO,NVIC,DMA etc.) are configured properly
/*
...
*/
/* UART register Receive complete user callback */
HAL_UART_RegisterCallback(&huart, HAL_UART_RX_CB_ID,
UART_RxCpltCallback);
/* Start a UART Process in dma mode */
HAL_UART_Receive_DMA(&huart, aRxBuffer,
DATA_TO_RECEIVE_SIZE)
|
#define DATA_TO_RECEIVE_SIZE 255
UART_HandleTypeDef huart;
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];
/* Ensure that all required peripherals
(UART, GPIO,NVI,DMA etc.) are configured properly
/*
...
*/
/* UART register Receive complete user callback */
HAL_UART_RegisterRxCpltCallback(&huart, UART_RxCpltCallback);
/* Start a UART Process in dma mode */
HAL_UART_Receive_DMA(&huart, aRxBuffer,
DATA_TO_RECEIVE_SIZE)
|
Key changes ¶
HAL2 DMA start uses
HAL_DMA_StartPeriphXfer_IT_Opt()(optional interrupt generation) instead ofHAL_DMA_Start_IT().HAL2 RX completion callback includes an RX event argument (for example
HAL_UART_RXEVENT_TC).
Use case: UART TX in DMA mode ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define DATA_TO_SEND_SIZE 255
UART_HandleTypeDef huart;
uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** UART Communication **** ";
/* Ensure that all required peripherals
(USART, GPIO,NVIC,DMA etc.) are configured properly
/*
...
*/
/* UART register Transmit complete user callback */
HAL_UART_RegisterCallback(&huart, HAL_UART_TX_CB_ID,
UART_TxCpltCallback);
/* Start a UART Process in dma mode */
HAL_UART_Transmit_DMA(&huart, aTxBuffer,
DATA_TO_SEND_SIZE)
|
#define DATA_TO_SEND_SIZE 255
hal_uart_handle_t huart;
uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** UART Communication **** ";
/* Ensure that all required peripherals
(USART, GPIO,NVIC,DMA etc.) are configured properly
/*
...
*/
/* UART register Transmit complete user callback */
HAL_UART_RegisterTxCpltCallback(&huart, UART_TxCpltCallback);
/* Start a UART Process in dma mode */
HAL_UART_Transmit_DMA(&huart, aTxBuffer,
DATA_TO_SEND_SIZE)
|
Key changes ¶
HAL2 DMA start uses
HAL_DMA_StartPeriphXfer_IT_Opt()(optional interrupt generation) instead ofHAL_DMA_Start_IT().HAL2 uses byte-oriented transfer count fields (for example
tx_xfer_count).
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
hal_uart_two_boards_com_dma_controller |
How to handle an infinite number of transmit-receive transactions between two boards based on the UART-bus protocol with the HAL API, in DMA mode. |
|
hal_uart_two_boards_com_dma_responder |
How to respond in a dma mode UART communication, driven by the controller, using the HAL API. |









